<template>
  <div class="redbags-box">
    <div class="redbags-box" v-show="showbags && showDialog">
      <div class="mark"></div>
      <div class="red-box">
        <div class="close" @click="closeFn">
          <img src="../../../assets/img/wx/close.png" alt />
        </div>
        <div class="info" @click="openFn">
          <img src="../../../assets/img/wx/redbags.png" alt />
          <p class="money">
            <b>{{money | formatMoney}}</b>
            <span>元</span>
          </p>
        </div>
      </div>
    </div>
    <div class="received-box" v-show="showbags && showDialog2 && !getBags">
      <div class="received-mark"></div>
      <div class="received-info">
        <img src="../../../assets/img/wx/received.png" alt />
        <div class="close-btn" @click="closeFn"></div>
        <div class="open-btn" @click="goCash"></div>
      </div>
    </div>
  </div>
</template>

<script>
import wechatService from 'SERVICE/wechatService'
import { mapGetters } from 'vuex'
export default {
  data() {
    return {
      money: '0',
      showbags: false,
      showDialog: false,
      showDialog2: false,
      uuid: '',
      enterpriseId: '',
      getBags: sessionStorage.getItem('getBags') || false
    }
  },
  created() {
    this.showbags = this.$route.query.showbags || sessionStorage.getItem('showbags') || false
    this.uuid = localStorage.getItem('uuid') || ''
    this.enterpriseId = localStorage.getItem('enterpriseId') || ''
    // 公众号请求红包接口
    if (sessionStorage.getItem('userLoginType') != 1) {
      this.agentRedPacket()
    }
  },
  computed: {
    ...mapGetters(['userInfo'])
  },
  methods: {
    agentRedPacket() {
      let agentId = (this.userInfo && this.userInfo.agentId) || ''
      wechatService
        .agentRedPacket({
          uuid: this.uuid,
          enterpriseId: this.enterpriseId,
          agentId: agentId,
          officialAccountOpenId: sessionStorage.getItem('officialAccountOpenId')
        })
        .then(res => {
          this.showDialog = !res.get && res.popup
          this.showDialog2 = res.get
          this.money = res.redPacketMax
        })
        .catch()
    },
    closeFn() {
      sessionStorage.setItem('getBags', true)
      this.showbags = false
    },
    openFn() {
      sessionStorage.setItem('getBags', true)
      this.$router.push(`/money/packets`)
    },
    goCash() {
      sessionStorage.setItem('getBags', true)
      if (!this.userInfo.agentId) {
        this.$router.push(`/wx/login?redPacketAmount=${this.money}&uuid=${this.uuid}&enterpriseId=${this.enterpriseId}&toPath=/money/wallet`)
      } else {
        this.$router.push('/money/wallet')
      }
    }
  },
  filters: {
    formatMoney(val) {
      if (val) {
        return (val / 100).toFixed(2)
      }
      return '0.00'
    }
  }
}
</script>

<style lang="less" scoped>
.redbags-box {
  .mark {
    background-color: rgba(0, 0, 0, 0.6);
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: 999;
  }
  .red-box {
    position: absolute;
    height: 370px;
    width: 340px;
    left: 50%;
    top: 50%;
    margin-left: -170px;
    margin-top: -185px;
    z-index: 9999;
    .close {
      img {
        width: 38px;
        height: 37px;
        position: absolute;
        right: 20px;
      }
    }
    .info {
      .money {
        position: absolute;
        width: 100%;
        top: 89px;
        font-size: 18px;
        color: #fd4f48;
        display: flex;
        align-items: flex-end;
        justify-content: center;
        b {
          font-size: 50px;
          color: rgba(253, 79, 72, 1);
        }
        span {
          margin-left: 3px;
          padding-bottom: 10px;
        }
      }
    }
  }
}
.received-box {
  .received-mark {
    background-color: rgba(0, 0, 0, 0.6);
    position: fixed;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    z-index: 999;
  }
  .received-info {
    z-index: 9999;
    width: 272px;
    height: 318px;
    position: absolute;
    left: 50%;
    top: 50%;
    margin-left: -136px;
    margin-top: -159px;
    .close-btn {
      position: absolute;
      width: 28px;
      height: 28px;
      top: 10px;
      right: 10px;
    }
    .open-btn {
      width: 175px;
      height: 36px;
      position: absolute;
      bottom: 20px;
      left: 50px;
    }
  }
}
</style>